package com.jason.platform.supervision.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.activity.LeaveActivity;
import com.jason.platform.supervision.adapter.LeaveApprovalAdapter;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.Category;
import com.jason.platform.supervision.beans.LeaveSignBean;
import com.jason.platform.supervision.beans.LeaveSignResp;
import com.jason.platform.supervision.task.QueryLeaveApprovalListTask;
import com.jason.platform.supervision.task.QueryLeaveSignListTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.jason.platform.supervision.widght.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: StatisticsVisitFragment
 * @Description: 请假审批
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015年12月1日10:17:58
 */
public class LeaveApprovalFragment extends BaseFragment{

    private static final String TAG = LeaveApprovalFragment.class.getSimpleName();

    private Category mParentCategory;
    private int mPosition;

    private ListView mListView;
    private LeaveApprovalAdapter mAdapter;

    private List<LeaveSignBean> mLeaveSignBeans = new ArrayList<LeaveSignBean>();
    private LeaveActivity mActivity;
    private View mFootView;

    private String mName;
    private String mPhone;

    //搜索
    private EditText mNameEditView, mPhoneEditView;
    private Button mSearchButton;

    private LoadingDialog mLoadingDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate...");
        mParentCategory = (Category) tag;
        if (arg1 != null) {
            mPosition = (Integer) arg1;
        }
        mActivity = (LeaveActivity) getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i(TAG,"onCreateView...");
        View view = inflater.inflate(R.layout.fragment_leave_approval, null, true);
        mListView = (ListView) view.findViewById(R.id.leave_approval_list);
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(mActivity, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.GONE);
        mListView.addFooterView(mFootView);
        mAdapter = new LeaveApprovalAdapter(mActivity);
        mListView.setAdapter(mAdapter);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == (view.getCount() - 1)) {//滚动到底部
                        if (!mIsLoading && mHasNextPage) {
                            loadMore();
                        } else {
                            if (mFootView != null) {
                                mFootView.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        initSearchView(view);
        initLoadingDialog();
        return view;
    }

    protected void initLoadingDialog() {
        mLoadingDialog = new LoadingDialog(mActivity);
        mLoadingDialog.setIsOut(false);
        mLoadingDialog.setMessage("加载中");
    }

    private void initSearchView(View view) {
        mNameEditView = (EditText) view.findViewById(R.id.search_header_name);
        mPhoneEditView = (EditText) view.findViewById(R.id.search_header_phone);
        mSearchButton = (Button) view.findViewById(R.id.search_header_search_btn);
        mSearchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 调用接口，进行搜索
                mLoadingDialog.show();
                initData(DataState.DATA_SEARCH);
            }
        });
    }

    @Override
    public void initData(int netState) {
        super.initData(netState);
        mIsLoading = true;
        mIsStart = true;
        mCurrentIndex = 0;
        if (netState != DataState.DATA_FIRST){
            getSearchValue();
        }
        if (netState == DataState.DATA_REFRESH){
            if (mLoadingDialog != null){
                mLoadingDialog.show();
            }
        }
        QueryLeaveApprovalListTask task = new QueryLeaveApprovalListTask(mActivity);
        task.type = Constant.Manager.GET_LEAVE_APPROVAL_LIST;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(mActivity).toString());
        task.mParams.put("name", mName);
        task.mParams.put("telephone", mPhone);
        task.arg2 = mPosition;
        task.execute();
    }

    private void getSearchValue(){
        mName = mNameEditView.getText().toString();
        mPhone = mPhoneEditView.getText().toString();
    }

    private void loadMore() {
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(mActivity, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.VISIBLE);
        mListView.setSelection((mListView.getAdapter().getCount() - 1));
        mIsLoading = true;
        mIsStart = false;
        getSearchValue();
        QueryLeaveSignListTask task = new QueryLeaveSignListTask(mActivity);
        task.type = Constant.Manager.GET_LEAVE_APPROVAL_LIST;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(mActivity).toString());
        task.mParams.put("name", mName);
        task.mParams.put("telephone", mPhone);
        task.arg2 = mPosition;
        task.execute();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume...");
    }

    @Override
    public void onFrameResume() {
        super.onFrameResume();
        Log.i(TAG,"onResume...");
    }

    @Override
    public void onDataCallback(Task task) {
        super.onDataCallback(task);
        if (task.type == Constant.Manager.GET_LEAVE_APPROVAL_LIST) {
            if ( mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                LeaveSignResp resp = (LeaveSignResp) task.resData;
                if (mIsStart){
                    mLeaveSignBeans.clear();
                }
                mLeaveSignBeans.addAll(resp.getValues());
                if (mLeaveSignBeans != null && !mLeaveSignBeans.isEmpty()){
                    if (mAdapter != null){
                        mAdapter.addAll(mLeaveSignBeans);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                mHasNextPage = !resp.isLastPage();
                mIsLoading = false;
                if (mHasNextPage) {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.GONE);
                    }
                }
            }else {
                if (mAdapter != null){
                    mAdapter.addAll(new ArrayList<LeaveSignBean>());
                    mAdapter.notifyDataSetChanged();
                }
                if (mFootView != null) {
                    mFootView.setVisibility(View.GONE);
                }
                Toast.makeText(mActivity,task.rspDesc,Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLoadingDialog != null){
            mLoadingDialog.dismiss();
        }
    }
}
